//
//  ComposeViewController.swift
//  仿一个新浪微博
//
//  Created by 孙家林 on 15/10/9.
//  Copyright © 2015年 sunjialin. All rights reserved.
//

import UIKit

class ComposeViewController: UIViewController {


    override func loadView() {
        
        view = UIView()
        //视图控制器，一定要设置颜色
        //如果动画切换视图控制器变慢，或者画面闪烁，重点检查是否设置颜色
        view.backgroundColor = UIColor.whiteColor()
        
        prepareNav()
        prepareToolbar()
        prepareTextView()
    }

    private func prepareNav() {
        
        navigationItem.leftBarButtonItem = UIBarButtonItem(title: "取消", style: UIBarButtonItemStyle.Plain, target: self, action: "close")
        navigationItem.rightBarButtonItem = UIBarButtonItem(title: "发送", style: UIBarButtonItemStyle.Plain, target: self, action: "sendStatus")
        
        navigationItem.rightBarButtonItem?.enabled = false
        
        //标题栏
        let titleView = UIView(frame: CGRect(x: 0, y: 0, width: 100, height: 32))
        
        let titleLabel = UILabel(color: UIColor.darkGrayColor(), fontSize: 15)
        titleLabel.text = "发微博"
        titleLabel.sizeToFit()
        titleView.addSubview(titleLabel)
        titleLabel.ff_AlignInner(type: ff_AlignType.TopCenter, referView: titleView, size: nil)
        
        let nameLabel = UILabel(color: UIColor.lightGrayColor(), fontSize: 13)
        nameLabel.text = UserAccount.loadAccount()?.name ?? ""
        nameLabel.sizeToFit()
        titleView.addSubview(nameLabel)
        nameLabel.ff_AlignInner(type: ff_AlignType.BottomCenter, referView: titleView, size: nil)
        
        navigationItem.titleView = titleView
    
    }
    
    //准备toolBar
    func prepareToolbar() {
        
        toolBar.backgroundColor = UIColor(white: 0.8, alpha: 1.0)
        
        view.addSubview(toolBar)
        
        //设置布局
        toolBar.ff_AlignInner(type: ff_AlignType.BottomLeft, referView: view, size: CGSize(width: UIScreen.mainScreen().bounds.width, height: 44))
        
        //定义一个数组
        let itemSettings = [["imageName": "compose_toolbar_picture"],
            ["imageName": "compose_mentionbutton_background"],
            ["imageName": "compose_trendbutton_background"],
            ["imageName": "compose_emoticonbutton_background","action": "inputEmoticon"],
            ["imageName": "compose_addbutton_background"]]
        
        var items = [UIBarButtonItem]()
        
        for dict in itemSettings {
            
            items.append(UIBarButtonItem(imageName: dict["imageName"]!, target: self, action: dict["action"]))
            
            
            items.append(UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FlexibleSpace, target: nil, action: nil))
        }
        
        items.removeLast()
        toolBar.items = items
    }
    
    //准备文本框
    func prepareTextView() {
    
        view.addSubview(textView)
        
        textView.backgroundColor = UIColor.whiteColor()
        
        // 提示：在 iOS 开发中，如果是单纯的 nav + scrollView 会自动调整滚动视图的边距
        // 会将顶部的导航栏的高度预留出来，就能实现滚动视图穿透效果
        textView.contentInset = UIEdgeInsets(top: 64, left: 0, bottom: 0, right: 0)
        textView.ff_AlignInner(type: ff_AlignType.TopLeft, referView: view, size: nil)
        textView.ff_AlignVertical(type: ff_AlignType.TopRight, referView: toolBar, size: nil)
        
        //添加占位标签
        placeholderLabel.text = "分享新鲜事..."
        textView.addSubview(placeholderLabel)
        placeholderLabel.sizeToFit()
        placeholderLabel.ff_AlignInner(type: ff_AlignType.TopLeft, referView: textView, size: nil, offset: CGPoint(x: 5, y: 8))
        
    }
    
    //取消发布微博
    func close() {
    
        dismissViewControllerAnimated(true, completion: nil)
    }
    
    //发布微博
    func sendStatus() {
    
        
    }
    
    //
    func inputEmoticon() {
    
        print("选择表情")
    }
    
    //懒加载
    private lazy var textView: UITextView = {
        
        let tv = UITextView()
        
        tv.font = UIFont.systemFontOfSize(15)
        
        tv.text = "分享新鲜事..."
        
        //允许垂直拖拽
        tv.alwaysBounceVertical = true
        
        //拖拽关闭键盘
        tv.keyboardDismissMode = UIScrollViewKeyboardDismissMode.OnDrag
        
        return tv
    }()
    
    //占位标签
    private lazy var placeholderLabel = UILabel(color: UIColor.lightGrayColor(), fontSize: 15)
    
    private lazy var toolBar = UIToolbar()
}
